/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * @author Xing Jiankuan
 *
 */
public class LogManager {

	private static boolean isInitialized = false;
	
	public static synchronized boolean isInitalized() {
		return isInitialized;
	}
	
	public static String DEFAULT_LOG_NAME = "dracots";
	
	public static String DEFAULT_PATH = "./log/";
	
	/**
	 * @param path the path of log file
	 * @param name The prefix name of log file
	 * @author Xing Jiankuan
	 */
	public static synchronized void init(String path, String name) {
		
		try {
			Date date = new Date();
			SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd_HHmmss_S");
			File pathF = new File(path);
			if(!pathF.exists()) {
				pathF.mkdirs();
			}
			String s = String.format("%s%s_%s.log", path, name, f.format(date));
			Logger logger = Logger.getLogger("sagroup.dracots");
			FileHandler fh = new FileHandler(s, 50000000, 10, true);
			fh.setLevel(Level.INFO);
			fh.setFormatter(new SimpleFormatter());
			logger.addHandler(fh); // add handler to file

		} catch (IOException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (Exception e) {
			// use default configuration of JRE
		} finally {
			isInitialized = true;
		}
		
	}
	
	/**
	 * Use Default log file name
	 * 
	 * @author Xing Jiankuan
	 */
	public static synchronized void init() {
		init(DEFAULT_PATH, DEFAULT_LOG_NAME);
	}
}
